import client.ClientListener;
import client.ClientQueue;
import client.ClientQueueHandler;
import databases.MongoDB;
import databases.MySQL;
import settings.Settings;
import utilities.ANSI;
import weatherstation.WeatherStationListener;
import weatherstation.WeatherStationQueue;
import weatherstation.WeatherStationQueueHandler;

/**
 * The Class Server.
 * 
 * @author Nils Berlijn
 * @author Henderikus Harms
 * @version 1.0
 * @since June 1, 2014
 */
public class Server 
{
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) 
	{
		try
		{
			System.out.println(ANSI.BLUE + "Server\n" + ANSI.NORMAL);	
			
			System.out.println(ANSI.YELLOW + "Server starting up...\n" + ANSI.NORMAL);
			
			System.out.println(ANSI.YELLOW + "Waiting for initializing the server modules...\n" + ANSI.NORMAL);

			initializeDatabases();
			initializeWeatherStationModules();
			initializeClientModules();
			
			System.out.println(ANSI.GREEN + "Server is successfully launched.\n" + ANSI.NORMAL);

			System.out.println(ANSI.BLUE + "Server Log\n" + ANSI.NORMAL);				
		}
		catch(Exception e)
		{
			System.out.println(ANSI.GREEN + "Server is not successfully launched.\n" + ANSI.NORMAL);			
		}
	}

	/**
	 * Initialize databases.
	 */
	private static void initializeDatabases()
	{
		System.out.println(ANSI.YELLOW + "Initializing Databases....\n" + ANSI.NORMAL);		
		
		MySQL.connect();
		MongoDB.connect();
		
		System.out.println(ANSI.GREEN + "All Databases have been successful initialized.\n" + ANSI.NORMAL);		
	}
	
	/**
	 * Initialize weather station modules.
	 */
	private static void initializeWeatherStationModules()
	{
		System.out.println(ANSI.YELLOW + "Initializing Weather Station modules....\n" + ANSI.NORMAL);
		
		WeatherStationQueue<Runnable> weatherStationQueue = new WeatherStationQueue<Runnable>(Settings.WEATHER_STATION_QUEUE_LIMIT);
		
		Thread weatherStationQueueHandler = new Thread(new WeatherStationQueueHandler(Settings.WEATHER_STATION_THREAD_POOL_SIZE, weatherStationQueue));
		weatherStationQueueHandler.start();
		
		Thread weatherStationListener = new Thread(new WeatherStationListener(Settings.WEATHER_STATION_LISTENER_PORT, weatherStationQueue));
		weatherStationListener.start();

		System.out.println(ANSI.GREEN + "All Weather Station modules have been successful initialized.\n" + ANSI.NORMAL);			
	}
	
	/**
	 * Initialize client modules.
	 */
	private static void initializeClientModules()
	{
		System.out.println(ANSI.YELLOW + "Initializing Client modules....\n" + ANSI.NORMAL);	
		
		ClientQueue<Runnable> clientQueue = new ClientQueue<Runnable>(Settings.CLIENT_QUEUE_LIMIT);
		
		Thread clientQueueHandler = new Thread(new ClientQueueHandler(Settings.CLIENT_THREAD_POOL_SIZE, clientQueue));	
		clientQueueHandler.start();
		
		Thread clientListener = new Thread(new ClientListener(Settings.CLIENT_LISTENER_PORT, clientQueue));
		clientListener.start();
		
		System.out.println(ANSI.GREEN + "All Client modules have been successful initialized.\n" + ANSI.NORMAL);		
	}
}